package com.android.powerbankpad.ui;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.powerbankpad.base.BaseFragment;
import com.android.powerbankpad.bean.ChildBean;
import com.android.powerbankpad.cache.ProxyVideoCacheManager;
import com.android.powerbankpad.databinding.FragmentImageBinding;
import com.danikula.videocache.HttpProxyCacheServer;
import com.ws.universal.tools.utils.LogcatUtils;
import com.ws.universal.tools.utils.glide.GlideUtils;

import io.reactivex.disposables.CompositeDisposable;
import xyz.doikki.videocontroller.StandardVideoController;
import xyz.doikki.videoplayer.player.VideoView;

public class MyFragment extends BaseFragment {
    public FragmentImageBinding binding;

    public StandardVideoController controller;
    private ChildBean childBean;


    public MyFragment(ChildBean childBean) {
        this.childBean = childBean;
    }


    @Override
    public View getRootView(LayoutInflater inflater, ViewGroup container) {
        binding = FragmentImageBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void initView() {
        controller = new StandardVideoController(getContext());
        //controller.addControlComponent(new CompleteView(getContext())); //自动完成播放界面
        //根据屏幕方向自动进入/退出全屏
        //controller.setEnableOrientation(true);
        binding.videoPlayer.setVideoController(controller);
        binding.videoPlayer.setScreenScaleType(VideoView.SCREEN_SCALE_MATCH_PARENT);
        if (childBean != null) {
            if (childBean.type.equals("image")) {
                GlideUtils.CreateImageRound(childBean.file, binding.imgAd);
                binding.imgAd.setVisibility(View.VISIBLE);
                binding.videoPlayer.setVisibility(View.GONE);
                //LogcatUtils.e(" 执行设置 图片url " + childBean.file);
            } else {
                LogcatUtils.e("MyFragment videoPlayer Source " + childBean.file);
                HttpProxyCacheServer cacheServer = ProxyVideoCacheManager.getProxy(getContext());
                String proxyUrl = cacheServer.getProxyUrl(childBean.file);
                //String proxyUrl = cacheServer.getProxyUrl("https://lechongbao.w-dian.cn/e4be5ab3743f2f875d93428cf224c6e6.mp4");
                binding.videoPlayer.setUrl(proxyUrl);
                // LogcatUtils.e(" 执行设置 视频url " + childBean.file);
                binding.imgAd.setVisibility(View.GONE);
                binding.videoPlayer.setVisibility(View.VISIBLE);
            }
        }


    }

    @Override
    public void autoData() {

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        // 释放资源，将引用置为 null
        if (childBean != null) {
            childBean = null;
        }
        if (binding != null) {
            if (binding.videoPlayer != null) {
                binding.videoPlayer.release();
            }
        }
        if (controller != null) {
            controller = null;
        }


    }


}
